package com.zego.wawaji.third.pay.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.toast.PToast;

import java.util.Map;

/**
 * 支付宝SDK调用
 */
public class PayAlipayUtils {
    private static final int SDK_PAY_FLAG = 1;
    private Activity activity;

    public PayAlipayUtils(Activity activity) {
        super();
        this.activity = activity;
    }

    /**
     * 判断有没有安装支付宝
     */
    public static boolean isAliPayInstalled(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }

    public void pay(final String payInfo) {
        // 发起支付
        Runnable payRunnable = () -> {
            PayTask alipay = new PayTask(activity);
            Map<String, String> result = alipay.payV2(payInfo, true);

            Message msg = new Message();
            msg.what = SDK_PAY_FLAG;
            msg.obj = result;
            mHandler.sendMessage(msg);
        };
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        PToast.showShort("支付成功");
                        //更新信息
                        MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
                        MsgMgr.getInstance().sendMsg(MsgType.MT_Recharge_Suc, null);
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        if (TextUtils.equals(resultStatus, "8000")) {
                            PToast.showShort("支付结果确认中");
                            //通知刷个人资料
                            MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
                        } else {
                            PToast.showShort("支付失败");
                        }
                    }
                    break;
                }
            }
        }
    };
}
